import 'dart:convert';
import 'dart:io';
import 'package:excel/excel.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_gbk2utf8/flutter_gbk2utf8.dart';

Future<String?> pickSgf() async {
  var result = await FilePicker.platform.pickFiles(
    allowedExtensions: ['sgf'],
    type: FileType.custom,
    lockParentWindow: true,
  );
  if (result != null && result.files.isNotEmpty) {
    String sgf;
    if (kIsWeb) {
      try {
        sgf = utf8.decode(result.files[0].bytes!);
      } catch (e) {
        sgf = gbk.decode(result.files[0].bytes!);
      }
    } else {
      try {
        sgf = await File(result.files[0].path!).readAsString();
      } catch (e) {
        var byteContent = File(result.files[0].path!).readAsBytesSync();
        sgf = gbk.decode(byteContent);
        // sgf = await File(result.files[0].path!).readAsString(encoding: systemEncoding);
      }
    }
    return sgf;
  }
  return null;
}

Future<String> sgfFromPath(String path) async {
  String sgf;
  try {
    sgf = await File(path).readAsString();
  } catch (e) {
    var byteContent = File(path).readAsBytesSync();
    sgf = gbk.decode(byteContent);
  }
  return sgf;
}

Future<List<List<Data?>>?> pickExcel() async {
  var result = await FilePicker.platform.pickFiles(
    allowedExtensions: ['xlsx'],
    type: FileType.custom,
    lockParentWindow: true,
  );
  print('result: $result');
  if (result != null && result.files.isNotEmpty) {
    var bytes = await File(result.files[0].path!).readAsBytesSync();
    // var decoder = SpreadsheetDecoder.decodeBytes(bytes);
    // print(decoder); // 获取数据
    var excel = Excel.decodeBytes(bytes);
    var sheet = excel.tables.keys.toList()[1];
    print('sheet: $sheet');
    List<List<Data?>> list = <List<Data>>[];
    list = excel.tables[sheet]!.rows;
    return list;
    for (var row in excel.tables[sheet]!.rows) {
      for (var cell in row) {
        if (cell == null) continue;
        final value = cell.value;
        final numFormat = cell.cellStyle?.numberFormat ?? NumFormat.standard_0;
        final bgColor = cell.cellStyle!.backgroundColor.colorHex;
        final font = cell.cellStyle!.fontFamily;
        final fontSize = cell.cellStyle!.fontSize;
        final textColor = cell.cellStyle!.fontColor.colorHex;
        final isBold = cell.cellStyle!.isBold;
        // switch (value) {
        //   case null:
        //     print(' ');
        //     print('  format: ${numFormat}');
        //   case TextCellValue():
        //     print('  text: ${value.value}');
        //   case FormulaCellValue():
        //     print('  formula: ${value.formula}');
        //     print('  format: ${numFormat}');
        //   case IntCellValue():
        //     print('  int: ${value.value}');
        //     print('  format: ${numFormat}');
        //   case BoolCellValue():
        //     print('  bool: ${value.value ? 'YES!!' : 'NO..'}');
        //     print('  format: ${numFormat}');
        //   case DoubleCellValue():
        //     print('  double: ${value.value}');
        //     print('  format: ${numFormat}');
        //   case DateCellValue():
        //     print(
        //         '  date: ${value.year} ${value.month} ${value.day} (${value.asDateTimeLocal()})');
        //   case TimeCellValue():
        //     print(
        //         '  time: ${value.hour} ${value.minute} ... (${value.asDuration()})');
        //   case DateTimeCellValue():
        //     print(
        //         '  date with time: ${value.year} ${value.month} ${value.day} ${value.hour} ... (${value.asDateTimeLocal()})');
        // }
        print(
            'Cell[${cell.rowIndex},${cell.columnIndex}]:$value, bgColor:$bgColor,font:$font,fontSize:$fontSize,textColor:$textColor,isBold:$isBold,format:$numFormat');
      }
    }
    // for (var table in excel.tables.keys) {
    //   print(table); //sheet Name
    //   print(excel.tables[table]!.maxColumns);
    //   print(excel.tables[table]!.maxRows);
    // for (var row in excel.tables[table]!.rows) {
    //   for (var cell in row) {
    //     print('cell ${cell!.rowIndex}/${cell.columnIndex}');
    //     final value = cell.value;
    //     final numFormat =
    //         cell.cellStyle?.numberFormat ?? NumFormat.standard_0;
    //     switch (value) {
    //       case null:
    //         print('  empty cell');
    //         print('  format: ${numFormat}');
    //       case TextCellValue():
    //         print('  text: ${value.value}');
    //       case FormulaCellValue():
    //         print('  formula: ${value.formula}');
    //         print('  format: ${numFormat}');
    //       case IntCellValue():
    //         print('  int: ${value.value}');
    //         print('  format: ${numFormat}');
    //       case BoolCellValue():
    //         print('  bool: ${value.value ? 'YES!!' : 'NO..'}');
    //         print('  format: ${numFormat}');
    //       case DoubleCellValue():
    //         print('  double: ${value.value}');
    //         print('  format: ${numFormat}');
    //       case DateCellValue():
    //         print(
    //             '  date: ${value.year} ${value.month} ${value.day} (${value.asDateTimeLocal()})');
    //       case TimeCellValue():
    //         print(
    //             '  time: ${value.hour} ${value.minute} ... (${value.asDuration()})');
    //       case DateTimeCellValue():
    //         print(
    //             '  date with time: ${value.year} ${value.month} ${value.day} ${value.hour} ... (${value.asDateTimeLocal()})');
    //     }
    //
    //     print('$row');
    //   }
    // }
    // }
  }

  return null;
}
